<?php
include_once 'model/category/process.php';
include_once 'module/controls.php';
include_once 'model/user/user_create.php';
$c = new controls();
$user = new users();
$fillByName = $user->FillOneByName($_SESSION);
$category = new categories();
$result = $category->FillAll();
$level_decode = json_decode($fillByName["level"], true);
?>
<div id="content">
    <?php if ($level_decode["admin"] != "" || $level_decode["create"] != "") { ?>
        <p><span class="bglinkleft"><a class="link" href="<?= $url["home"] ?>/menu/categorycreate/t/0"><?php echo $phrase["create_cate"] ?></a></span></p>
    <?php } ?>
    <form method="post" action="<?= $url["home"] ?>/menu/categoryprocess/t/0">
        <p>
        <ul id="sortable">
            <?php
            while ($rows = mysql_fetch_array($result)) {
                ?>
                <li class="ui-state-default-cate"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>
                    <?php
                        $value = array("cate_id" => $rows["category_id"], "order" => $rows["category_order"]);
                    ?>
                    <input type="hidden" value='<?php print_r(json_encode($value)) ?>' name="cate_name[]" id="cate_name[]"/>
                    <span class="right">
                        <?php if ($level_decode["admin"] != "" || $level_decode["update"] != "") { ?>
                            <a href="<?= $url["home"] ?>/menu/cateupdate/t/0/id/<?php echo $rows["category_id"] ?>">
                                <image src="<?= $url["home"] ?>/images/icons/edit-icon.gif"/>
                            </a>
                        <?php } ?>
                        <?php if ($level_decode["admin"] != "" || $level_decode["update"] != "" && $level_decode["delete"]) { ?>
                            | 
                        <?php } ?>
                        <?php if ($level_decode["admin"] != "" || $level_decode["delete"] != "") { ?>
                            <a href="<?= $url["home"] ?>/menu/catedelete/t/0/id/<?php echo $rows["category_id"] ?>" onclick="return confirm('Bạn có chắc chắn muốn xóa không.');">
                                <image src="<?= $url["home"] ?>/images/icons/b_drop.png"/>
                            </a>
                        <?php } ?>
                    </span>
                    <span class="center">
                        <?php echo $rows["category_name"] ?>
                    </span>
                </li>
            <?php } ?>
            <?php if (mysql_num_rows($result) > 0) { ?>
                <?php echo $c->get_button("updateorder", "update") ?>
            <?php } ?>
        </ul>
        </p>
    </form>

    <script>
        $(function() {
            $( "#sortable" ).sortable({
                start: function(event, ui) {
                    var start_pos = ui.item.index();
                    ui.item.data('start_pos', start_pos);
                },
                change: function(event, ui) {
                    var start_pos = ui.item.data('start_pos');
                    var index = ui.placeholder.index();
                    if (start_pos < index) {
                        $('#sortable li:nth-child(' + index + ')').addClass('highlights');
                    } else {
                        $('#sortable li:eq(' + (index + 1) + ')').addClass('highlights');
                    }
                },
                update: function(event, ui) {
                    $("li.ui-state-default-cate").each(function(index){
                        var oS = $.parseJSON($('li.ui-state-default-cate').eq(index).children('input').val());
                        var arrS = '{"cate_id":"'+oS.cate_id+'","order":"'+index+'"}';
                        $('li.ui-state-default-cate').eq(index).children('input').val(arrS);
                    });
                }
            });
            $("#sortable").disableSelection(); 
        });
    </script>
</div>